import fetch from 'node-fetch';

async function testAddCategoryDuplication() {
    console.log('🧪 测试新增文章类别的重复性检查\n');
    
    const baseUrl = 'http://localhost:8000/api/v1/admin';
    const authToken = 'Bearer admin_token';
    
    try {
        // 1. 测试重复的name（使用已存在的"游戏推荐"）
        console.log('🔥 测试1: 添加重复的name');
        const response1 = await fetch(`${baseUrl}/category/add`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': authToken
            },
            body: 'name=游戏推荐&slug=new-game-recommend'
        });
        const result1 = await response1.json();
        console.log(`状态码: ${response1.status}`);
        console.log(`响应: ${JSON.stringify(result1)}\n`);
        
        // 2. 测试重复的slug（使用已存在的"游戏推荐"）
        console.log('🔥 测试2: 添加重复的slug');
        const response2 = await fetch(`${baseUrl}/category/add`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': authToken
            },
            body: 'name=新游戏推荐&slug=游戏推荐'
        });
        const result2 = await response2.json();
        console.log(`状态码: ${response2.status}`);
        console.log(`响应: ${JSON.stringify(result2)}\n`);
        
        // 3. 测试参数为空
        console.log('🔥 测试3: 参数为空');
        const response3 = await fetch(`${baseUrl}/category/add`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': authToken
            },
            body: 'name=&slug='
        });
        const result3 = await response3.json();
        console.log(`状态码: ${response3.status}`);
        console.log(`响应: ${JSON.stringify(result3)}\n`);
        
        // 4. 测试正常添加（不重复的name和slug）
        console.log('🔥 测试4: 正常添加');
        const response4 = await fetch(`${baseUrl}/category/add`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': authToken
            },
            body: 'name=测试分类&slug=test-category'
        });
        const result4 = await response4.json();
        console.log(`状态码: ${response4.status}`);
        console.log(`响应: ${JSON.stringify(result4)}\n`);
        
        console.log('✅ 新增分类重复性检查测试完成！');
        
    } catch (error) {
        console.error('测试失败:', error.message);
    }
}

testAddCategoryDuplication();
